/*
 * How would you design a stack which, in addition to push and pop,
 * also has a function min which returns the minimum element?
 * Push, pop and min should all operate in O(1) time.
 */
package Chapter3;
import Chapter2.Node;
public class Chapter3_2 extends MyStack {
	MyStack minStack;
	public Chapter3_2(){
		minStack = new MyStack();
	}
	
	public void push(int item)
	{
		if(item <= min())
			minStack.push(item);
		super.push(item);
	}
	
	
	public Node pop(){
		Node node = super.pop();
		if( node.value == min())
			minStack.pop();
		return node;
	}
	
	public int min()
	{
		if(minStack.isEmpty())
		{
			return Integer.MAX_VALUE;
		}
		else
		{
			return minStack.peek().value;
		}
	}

}
